package com.itheima.controller;


/**
 * 体检检查项管理
 */

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;

import com.itheima.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import java.util.List;

@RestController//相当于controller+responseBody
@RequestMapping("/checkItem")
public class CheckItemController {

    @Reference
    private CheckItemService checkItemService;

    @Autowired
    private JedisPool jedisPool;

    //新增检查项
    @PreAuthorize("hasAnyAuthority('CHECKITEM_ADD')")//权限校验
    @RequestMapping("/add")
    public Result add(@RequestBody CheckItem checkItem) {
        try {
            checkItemService.add(checkItem);
            redisUtil(RedisConstant.ADD_CHECKITEM);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL);
        }
        return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
    }

    //分页查询
    @PreAuthorize("hasAnyAuthority('CHECKITEM_QUERY')")//权限校验
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = null;
        try {
            pageResult = checkItemService.pageQuery(queryPageBean.getCurrentPage(), queryPageBean.getPageSize(), queryPageBean.getQueryString());
            redisUtil(RedisConstant.SELECT_CHECKITEM);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pageResult;
    }

    //删除数据
    @PreAuthorize("hasAnyAuthority('CHECKITEM_DELETE')")//权限校验
    @RequestMapping("/delete")
    public Result delete(Integer id) {
        try {
            checkItemService.deleteById(id);
            redisUtil(RedisConstant.DELETE_CHECKITEM);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_CHECKITEM_FAIL);
        }
        return new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
    }

    //根据id查询数据
    @PreAuthorize("hasAnyAuthority('CHECKITEM_QUERY')")//权限校验
    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
            CheckItem checkItem = checkItemService.findById(id);
            redisUtil(RedisConstant.SELECT_CHECKITEM);
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, checkItem);
        } catch (RuntimeException e) {
            //处理一下service的RuntimeException，防止控制台报错
            e.printStackTrace();
            return new Result(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    //编辑体检项
    @PreAuthorize("hasAnyAuthority('CHECKITEM_EDIT')")//权限校验
    @RequestMapping("/edit")
    public Result edit(@RequestBody CheckItem checkItem) {
        try {
            checkItemService.edit(checkItem);
            redisUtil(RedisConstant.UPDATE_CHECKITEM);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_CHECKITEM_FAIL);
        }
        return new Result(true, MessageConstant.EDIT_CHECKITEM_SUCCESS);
    }

    //查询所有检查项
    @PreAuthorize("hasAnyAuthority('CHECKITEM_QUERY')")//权限校验
    @RequestMapping("/findAll")
    public Result findAll() {
        try {
            List<CheckItem> checkItemList = checkItemService.findAll();
            redisUtil(RedisConstant.SELECT_CHECKITEM);
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, checkItemList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    public  void redisUtil(String name) throws Exception {
        if (jedisPool.getResource().zrank(DateUtils.parseDate2String(DateUtils.getToday()), name) == null) {
            jedisPool.getResource().zadd(DateUtils.parseDate2String(DateUtils.getToday()), 1, name);
        } else {
            Double zscore = jedisPool.getResource().zscore(DateUtils.parseDate2String(DateUtils.getToday()), name);
            jedisPool.getResource().zadd(DateUtils.parseDate2String(DateUtils.getToday()), zscore + 1, name);
        }
    }
}